package com.bailian.app.controller.Apis;

import com.bailian.common.annotation.Anonymous;
import com.bailian.common.core.controller.BaseController;
import com.bailian.common.core.domain.AjaxResult;
import com.bailian.common.core.page.TableDataInfo;
import com.bailian.system.domain.BlPark;
import com.bailian.system.domain.BlParkArea;
import com.bailian.system.domain.BlWarehouse;
import com.bailian.system.domain.vo.BlParkVo;
import com.bailian.system.domain.vo.BlParkAreaVo;
import com.bailian.system.domain.vo.BLWareHouseVo;
import com.bailian.system.service.IBlParkService;
import com.bailian.system.service.IBlParkAreaService;
import com.bailian.system.service.IBlWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 园区控制器
 * 
 * @author bailian
 */
@Api(tags = "园区相关接口")
@RestController
@RequestMapping("/app/api/park")
public class ParkController extends BaseController {

    @Autowired
    private IBlParkService parkService;
    
    @Autowired
    private IBlParkAreaService parkAreaService;
    
    @Autowired
    private IBlWarehouseService warehouseService;

    /**
     * 获取园区列表
     */
    @Anonymous
    @ApiOperation("获取园区列表")
    @GetMapping("/list")
    public TableDataInfo getParkList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("搜索关键词") @RequestParam(required = false) String keyword) {
        
        startPage();
        BlPark park = new BlPark();
        if (keyword != null && !keyword.isEmpty()) {
            park.setParkName(keyword);
        }
        
        List<BlParkVo> list = parkService.selectList(park);
        return getDataTable(list);
    }

    /**
     * 获取园区区域
     */
    @Anonymous
    @ApiOperation("获取园区区域")
    @GetMapping("/areaList")
    public AjaxResult getParkAreaList() {
        BlParkArea area = new BlParkArea();
        List<BlParkAreaVo> list = parkAreaService.selectBlParkAreaVoList(area);
        return success(list);
    }
    
    /**
     * 获取园区详情
     */
    @Anonymous
    @ApiOperation("获取园区详情")
    @GetMapping("/{id}")
    public AjaxResult getParkDetail(@ApiParam("园区ID") @PathVariable Long id) {
        try {
            BlParkVo park = parkService.selectById(id);
            if (park == null) {
                return error("园区不存在");
            }
            return success(park);
        } catch (Exception e) {
            return error("获取园区详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取园区下所有推荐仓库
     */
    @Anonymous
    @ApiOperation("获取园区下所有推荐仓库")
    @GetMapping("/{parkId}/recommend/warehouses")
    public AjaxResult getRecommendWarehouses(
            @ApiParam("园区ID") @PathVariable Long parkId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        
        try {
            // 验证园区是否存在
            BlParkVo park = parkService.selectById(parkId);
            if (park == null) {
                return error("园区不存在");
            }
            
            // 设置分页
            startPage();
            
            // 构建查询条件：指定园区ID且为推荐状态
            BlWarehouse warehouse = new BlWarehouse();
            warehouse.setParkId(parkId);
            warehouse.setIsRecommend(1); // 1表示推荐
            
            // 查询推荐仓库列表
            List<BLWareHouseVo> list = warehouseService.selectBlWarehouseVoList(warehouse);
            
            return AjaxResult.success(list);
        } catch (Exception e) {
            return error("获取园区推荐仓库失败：" + e.getMessage());
        }
    }
} 